package com.neu.controller;

import com.neu.base.Result;
import com.neu.entity.Cost;
import com.neu.service.impl.BenchmarkServiceImpl;
import com.neu.service.impl.CostServiceImpl;
import com.neu.utils.CostUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/cost")
@Tag(name = "最终造价计算")
public class CostController {

    @Autowired
    private CostServiceImpl costService;

    @Autowired
    private BenchmarkServiceImpl benchmarkService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/benchmark")
    @Operation(summary = "城市基准")
    public Result getBenchmark(){
        return Result.success(benchmarkService.list());
    }

    @PostMapping("/workload")
    @Parameters({
            @Parameter(name = "s", description = "功能点数量", required = true),
            @Parameter(name = "pdr", description = "生产率", required = true),
            @Parameter(name = "rdf", description = "开发因素", required = true),
            @Parameter(name = "swf", description = "软件因素", required = true)
    })
    @Operation(summary = "工期计算")
    public Result<Float> workloadCalc(
            @RequestParam(defaultValue = "200.0") Float s,
            @RequestParam Float pdr,
            @RequestParam Float rdf,
            @RequestParam Float swf
    ){

        Float workload = CostUtil.CalcWorkLoad(s,pdr,rdf,swf);
        return Result.success(workload);
    }

    @PostMapping("/update")
    @Operation(summary = "更新成本")
    public Result updateCost(@RequestBody Cost cost){
        try{
            cost.calcESDC();
            cost.setProgress(1);
        }catch (Exception e){
            cost.setProgress(0);
        }

        Boolean sign = costService.updateById(cost);
        if(!sign){
            cost.setDelFlag("1");
            costService.save(cost);
        }

        return Result.success(sign);
    }

    @PostMapping("/get")
    @Parameter(name = "id", description = "成本ID", required = true)
    @Operation(summary = "获取成本")
    public Result getCost(@RequestParam String id){
        Cost cost = costService.getById(id);
        if(cost==null){
            cost = new Cost();
            cost.setProjectId(id);
            cost.setProgress(0);
            cost.setDelFlag("1");
            costService.save(cost);
        }
        return Result.success(costService.getById(id));
    }

    @PostMapping("/add")
    @Operation(summary = "插入")
    public Result insertCost(@RequestBody Cost cost){

        return Result.success(costService.save(cost));
    }



}
